const { SuccessModel, ErrorModel } = require("../../model/resModel");
const { getList, getDetail, createNewUser, updateUser, deleteUser, userLogin, userPay } = require('../../controllers/admin/user');

const handleUserRoute = (req, res) => {
    const method = req.method;
    const id = req.query.id;
    const userData = req.body;

    //新建用户
    if (method === 'POST' && req.path === '/api/user/new') {
        const newUserDataPromise = createNewUser(userData);
        return newUserDataPromise.then(newUserData => {
            return new SuccessModel(newUserData);
        })
        
    }

    //删除用户
    if (method === 'POST' && req.path === '/api/user/delete') {
        const deletedUserPromise = deleteUser(id);
        return deletedUserPromise.then(deletedUserData => {
            if (deletedUserData) {
                return new SuccessModel(`删除用户${id}成功`);
            } else {
                return new ErrorModel('删除失败');
            }
        })
        
    }

    //更新用户
    if (method === 'POST' && req.path === '/api/user/update') {
        const updatedUserPromise = updateUser(id, userData);
        return updatedUserPromise.then(updatedUserData => {
            if (updatedUserData) {
                return new SuccessModel('更新用户成功！！！');
            } else {
                return new ErrorModel('更新用户失败');
            }
        })
        
    }

    //支付扣款
    if (method === 'POST' && req.path === '/api/user/pay') {
        const updatedUserPromise = userPay(id, userData);
        return updatedUserPromise.then(updatedUserData => {
            if (updatedUserData) {
                return new SuccessModel('更新用户成功！！！');
            } else {
                return new ErrorModel('更新用户失败');
            }
        })
    }

    //用户登录
    if (method === 'POST' && req.path === '/api/user/login') {
        const userName = userData.username;
        const password = userData.password;
        const userloginPromise = userLogin(userName, password);
        return userloginPromise.then(loginResult => {
            // console.log(loginResult);
            if (!loginResult[0]) {
                return new SuccessModel(null);
            }
            if (loginResult[0].user_password == password) {
                return new SuccessModel({
                    userId: loginResult[0].user_id,
                    userName: loginResult[0].user_account,
                    email: loginResult[0].email,
                    phone: loginResult[0].phone,
                    money: loginResult[0].user_money,
                    addr:loginResult[0].user_addr
                }, 'true');
            }
            return new SuccessModel(false);
        });

    }

    //获取用户列表
    if (method === 'GET' && req.path === '/api/user/list') {
        const listDataPromise = getList();
        return listDataPromise.then(listData => {
            return new SuccessModel(listData);
        });
    }

    //获取用户详情
    if (method === 'GET' && req.path === '/api/user/detail') {
        const detailDataPromise = getDetail(id);
        return detailDataPromise.then(detailData => {
            return new SuccessModel(detailData[0]);
        })
    }
}

module.exports = handleUserRoute;